/*
 *  FoodTree.cpp
 *  Meal Wheel
 *
 *  Created by Justin Dell on 7/4/08.
 *  Copyright 2008 . All rights reserved.
 *
 */

#include "FoodTree.h"
#include <iostream>
#include <fstream>
using namespace std;

/**************** Food *******************/

// Constructors
Food::Food() {
    mName = "";
}
    
Food::Food(string name, vector<string> types) {
    mName = name;
    mTypes = types;
}
    
// Destructor
Food::~Food() {
    mTypes.clear();
}
    
void Food::addType(string type) {
    mTypes.push_back(type);
}
    
void Food::setName(string name) {
    mName = name;
}
    
string Food::getName() {
    return mName;
}
    
vector<string> Food::getTypes() {
    return mTypes;
}
    
// Copy - not to be used
void Food::operator=(Food copy) {
    return; 
}

/**************** Food Tree *******************/

// Constructor
FoodTree::FoodTree() {

}
    
// Destructor
FoodTree::~FoodTree() {
    mFoods.clear();
}
    
vector<Food *> FoodTree::getFoodsByType(string type) {
    vector<Food *> specifics;
    for (int i = 0; i < mFoods.size(); i++) {
        vector<string> types = mFoods[i]->getTypes();
        for (int j = 0; j < types.size(); j++) {
            if (types[j] == type) {
                specifics.push_back(mFoods[i]);
                break;
            }
        }
    }
    return specifics;
}

Food* FoodTree::getFoodByName(string name) {
    for (int i = 0; i < mFoods.size(); i++) {
        if (mFoods[i]->getName() == name) {
            return mFoods[i];
        }
    }
    
    Food *notFound;
    return notFound;
}

Food* FoodTree::getFoodByIndex(int index) {
    return mFoods.at(index);
}


void FoodTree::addFood(Food *xFood) {
    mFoods.push_back(xFood);
}

void FoodTree::addFood(string name) {
    Food *newFood = new Food();
    newFood->setName(name);
    mFoods.push_back(newFood);
}

void FoodTree::addFood(string name, vector<string> types) {
    Food *newFood = new Food(name, types);
    mFoods.push_back(newFood);
}

void FoodTree::removeFood(string name) {
    for (int i = 0; i < mFoods.size(); i++) {
        if (mFoods[i]->getName() == name) {
            mFoods.erase(mFoods.begin() + i);
            return;
        }
    }
}

void FoodTree::addTypeToFood(string type) {
    mFoods.at(mFoods.size() - 1)->addType(type);
}

void FoodTree::addTypeToFood(string type, string name) {
    for (int i = 0; i < mFoods.size(); i++) {
        if (mFoods[i]->getName() == name) {
            mFoods[i]->addType(type);
        }
    }
}

void FoodTree::writeXML(const char *path) {
    ofstream file;
    file.open(path);
    
    // Write header
    file << "<Foodlist>\n";
    
    // Write foods
    for(int i = 0; i < mFoods.size(); i++) {
        file << "\t<Food>\n";
        file << "\t\t<Name>";
        file << (mFoods[i]->getName());
        file << "</Name>\n";
        vector<string> types = mFoods[i]->getTypes();
        for (int j = 0; j < types.size(); j++) {
            file << "\t\t<Type>";
            file << types[j];
            file << "</Type>\n";
        }
        file << "\t</Food>\n";
    }
    
    // Write footer
    file << "</Foodlist>\n";
    file.close();
}

int FoodTree::size() {
    return mFoods.size();
}
    
// Copy - not to be used
void FoodTree::operator=(FoodTree copy) {

}